//
//  Net2.m
//  shou6
//
//  Created by FrankChen on 13-1-24.
//  Copyright (c) 2013年 FrankChen. All rights reserved.
//

#import "Net2.h"

@implementation Net2


-(NSString *) getHtml:(NSString *)webUrl{

    NSURL *url = [NSURL URLWithString:webUrl];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    NSString *retStr =[[NSString alloc]init];
    
    retStr=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    if([retStr isEqual:@""]){
        [self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];
    }
    
    return retStr;
}

-(void)showAlert{
    UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alert show];
}


-(NSString *) getData:(NSString *)html{
    
    NSString *tag1=[[[NSString alloc]init]autorelease];
    tag1=@"<td bgcolor=\"#FFFFFF\" align=\"left\" id=\"content\">";
    
    NSString *tag2=[[[NSString alloc]init]autorelease];
    tag2=@"</td>";
    
    NSRange range1=[html rangeOfString:tag1];
    
    NSString *content=[[[NSString alloc]init]autorelease];
    content=[html substringFromIndex:(range1.length+range1.location)];
    
    NSRange range2=[content rangeOfString:tag2];
    
    content=[content substringToIndex:range2.location];
    
    NSString *tag3=@"<p";
    NSRange range3=[content rangeOfString:tag3];
    
    if (range3.length!=0) {
        content=[content substringFromIndex:range3.location];
    }
    
    
    content=[self filterHtmlTag:content];
    content=[self filterKongGeHuiChe:content];
    
    content=[content stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    
    return content;
    
}

-(NSString *)filterKongGeHuiChe:(NSString *)content{
    NSRange range5;
    NSRange range6;
    NSRange range7;
    Boolean stop=false;
    while (!stop) {
        range7=[content rangeOfString:@"\r"];
        range5=[content rangeOfString:@" "];
        range6=[content rangeOfString:@"\n"];
        if(range5.location!=0&&range6.location!=0&&range7.location!=0){
            stop=true;
        }
        else{
            content=[content substringFromIndex:1];
        }
    }
    return content;
}

- (NSString *)filterHtmlTag:(NSString *)originHtmlStr{
    NSString *result = @"";
    NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<"];
    if (arrowTagStartRange.location != NSNotFound) { //如果找到
        NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@">"];
        //        NSLog(@"start-> %d   end-> %d", arrowTagStartRange.location, arrowTagEndRange.location);
        //        NSString *arrowSubString = [originHtmlStr substringWithRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location)];
        result = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location + 1) withString:@""];
        // NSLog(@"Result--->%@", result);
        return [self filterHtmlTag:result];    //递归，过滤下一个标签
    }else{
        result = [originHtmlStr stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];  // 过滤&nbsp等标签
        //result = [originHtmlStr stringByReplacingOccurrencesOf  ........
        //result=[result stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"];
        
    }
    return result;
}

@end
